﻿//Mirco Töpfer

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using pheidippides.app.Forms;
using pheidippides.configuration.Interfaces;
using pheidippides.logging.Classes;
using pheidippides.exceptionmanager.Classes;
using System.Threading;
using pheidippides.configuration.Classes;

namespace pheidippides.app.FormManager
{
    public delegate void AppInitialize();

    public class FormManager
    {
        #region Singleton

        private static FormManager staticFormManager;

        public static FormManager GetInstance()
        {
            if (staticFormManager == null)
                throw new ApplicationException("Der Formmanager ist nicht initialisiert!");
            return staticFormManager;
        }

        public static void Initialize(Form MDIMainForm)
        {
            staticFormManager = new FormManager(MDIMainForm);
        }

        private FormManager(Form MDIMainForm)
        {
            mdiMainForm = MDIMainForm;
            forms = new Dictionary<FormName, Form>();
        }

        #endregion

        #region Private Members

        private readonly Form mdiMainForm;
        private readonly Dictionary<FormName, Form> forms;

        #endregion

        #region Public Events

        public event AppInitialize OnAppInitialize;

        #endregion

        #region Public Methods

        public void ShowForm(FormName FormName)
        {
            Logger.GetInstance().LogMessage("Anfrage für Form " + FormName + " wird bearbeitet", LogLevel.Debug);
            try
            {
                if (!forms.ContainsKey(FormName))
                {
                    // Test CreateNewFormInThread(FormName);
                    CreateNewForm(FormName);
                }

                Form UsedForm = forms[FormName];

                UsedForm.Show();
            }
            catch (Exception ex)
            {
                Logger.GetInstance().LogException("Fehler beim generieren des Forms " + FormName, ex,
                                                  LogLevel.Exception);
                ExceptionManager.GetInstance().RegisterException(
                    "Fehler beim generieren des Forms " + FormName, ex);
            }
        }

        public void DisposeForm(Form DelForm)
        {
            try
            {
                foreach (KeyValuePair<FormName, Form> Frm in forms)
                {
                    if (DelForm.Equals(Frm.Value))
                    {
                        DisposeForm(Frm.Key);
                        return;
                    }
                }
                throw new ApplicationException("Form " + DelForm +
                                               " konnte nicht in der Formverwaltung gefunden werden");
            }
            catch (Exception ex)
            {
                Logger.GetInstance().LogException(
                    "Form " + DelForm + " konnte nicht disposed werden, Form nicht gefunden", ex,
                    LogLevel.Exception);
                ExceptionManager.GetInstance().RegisterException(
                    "Form " + DelForm + " konnte nicht disposed werden, Form nicht gefunden", ex);
            }
        }

        public void DisposeForm(FormName DelForm)
        {
            Logger.GetInstance().LogMessage("Form " + DelForm + " wird disposed", LogLevel.Debug);

            try
            {
                Form DisposeForm = forms[DelForm];
                forms.Remove(DelForm);
                DisposeForm.Dispose();
            }
            catch (Exception ex)
            {
                Logger.GetInstance().LogException(
                    "Form " + DelForm + " konnte nicht disposed werden, Form nicht gefunden", ex,
                    LogLevel.Exception);
                ExceptionManager.GetInstance().RegisterException(
                    "Form " + DelForm + " konnte nicht disposed werden, Form nicht gefunden", ex);
            }
        }

        internal Form GetMDIContainer()
        {
            return mdiMainForm;
        }

        public void InitializeApplication()
        {
            if (OnAppInitialize != null)
                OnAppInitialize();
        }

        #endregion

        #region Private Methods

        private void CreateNewForm(Object _FormName)
        {
            CreateNewForm((FormName) _FormName);
        }

        private void CreateNewForm(FormName FormName)
        {
            Logger.GetInstance().LogMessage("Generierung Form " + FormName, LogLevel.Debug);

            Form NewForm = null;

            switch (FormName)
            {
                case FormName.ConfigSettings:
                    NewForm = new ConfigForm();
                    break;
                case FormName.InfoWebsiteForm:
                    NewForm =
                        new BrowserForm(
                            ConfigManager.GetInstance().GetConfigEnry(ConfigEntryName.PheidippidesInfoWebsiteURL),
                            "Informationen zu Pheidippides");
                    break;
                case FormName.BugFeatureWebsiteForm:
                    NewForm =
                        new BrowserForm(
                            ConfigManager.GetInstance().GetConfigEnry(ConfigEntryName.PheidippidesBugFeatureWebsiteURL),
                            "Pheidippides Bug & Issues");
                    break;
                case FormName.LicenseWebsiteForm:
                    NewForm =
                        new BrowserForm(
                            ConfigManager.GetInstance().GetConfigEnry(ConfigEntryName.PheidippidesLicenseWebsiteURL),
                            "Pheidippides Lizenz");
                    break;
                case FormName.LoggingMonitorForm:
                    NewForm = new LoggingForm();
                    break;
                case FormName.TeilnehmerManagementForm:
                    NewForm = new TeilnehmerListForm();
                    break;
                default:
                    break;
            }

            if (NewForm != null)
            {
                NewForm.MdiParent = mdiMainForm;
                forms.Add(FormName, NewForm);
            }
        }

        private void CreateNewFormInThread(FormName FormName)
        {
            ParameterizedThreadStart ThreadStart = CreateNewForm;

            Thread FrmWorker = new Thread(ThreadStart);

            FrmWorker.Start(FormName);
        }

        #endregion
    }
}